/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.ImageBank.Query;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Interfaces.Tagging;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex
{
    public partial class TagImageSelectionForm : FormTemplate
    {
        private IImageTag m_searchTag;
        private Dictionary<int, IImageData> m_imageFilteredList;

        public TagImageSelectionForm(IImageTag searchTag)
        {
            InitializeComponent();

            m_imageFilteredList = new Dictionary<int, IImageData>();
            m_searchTag = searchTag;

            defaultIconsThumbnailViewer.ThumbnailMouseDoubleClickEvent += new EventHandler<ThumbnailMouseEventArgs>(defaultIconsThumbnailViewer_ThumbnailMouseDoubleClickEvent);
            myPhotoIndexThumbnailViewer1.ThumbnailMouseClickEvent += new EventHandler<ThumbnailMouseEventArgs>(myPhotoIndexThumbnailViewer1_ThumbnailMouseClickEvent);
            defaultIconsThumbnailViewer.ThumbnailMouseClickEvent += new EventHandler<ThumbnailMouseEventArgs>(defaultIconsThumbnailViewer_ThumbnailMouseClickEvent);
        }

        //public TagImageSelectionForm(Dictionary<int, IImageData> imageList)
        //{
        //    InitializeComponent();

        //    m_imageFilteredList = imageList;
        //}

        private void TagImageSelectionForm_Load(object sender, EventArgs e)
        {
            BuildImageList();
        }

        public IImageData SelectedImage
        {
            get
            {
                if (myPhotoIndexThumbnailViewer1.SelectedImages.Count == 0 &&
                    defaultIconsThumbnailViewer.SelectedImages.Count == 0)
                {
                    return null;
                }

                if (defaultIconsThumbnailViewer.SelectedImages.Count > 0)
                {
                    return defaultIconsThumbnailViewer.ImageList[defaultIconsThumbnailViewer.SelectedImages[0]];
                }

                return myPhotoIndexThumbnailViewer1.ImageList[myPhotoIndexThumbnailViewer1.SelectedImages[0]];
            }
        }

        private void BuildImageList()
        {
            if (m_searchTag != null)
            {
                ImageTagQueryParameter[] queryParameters =
                    new ImageTagQueryParameter[] { new ImageTagQueryParameter(m_searchTag.Id) };

                m_imageFilteredList.Clear();
                QueryBuilder queryBuilder = new QueryBuilder(queryParameters, new QueryHelper(Storage.Instance.UnFiltered), true);
                m_imageFilteredList = queryBuilder.RunQuery();
            }

            if (m_imageFilteredList.Count == 0)
            {
                myPhotoIndexThumbnailViewer1.ImageList = new QueryHelper(Storage.Instance.UnFiltered);
                foreach(IImageData imageData in Storage.Instance)
                {
                    m_imageFilteredList.Add(imageData.Id, imageData);
                }
            }
            else
            {
                myPhotoIndexThumbnailViewer1.ImageList = new QueryHelper(m_imageFilteredList);
            }

            myPhotoIndexThumbnailViewer1.Refresh();

            defaultIconsThumbnailViewer.ImageList = new TagIconsImageCollection();
        }

        private void myPhotoIndexThumbnailViewer1_ThumbnailMouseDoubleClickEvent(object sender, MyPhotoIndex.Controls.ThumbnailMouseEventArgs e)
        {
            defaultIconsThumbnailViewer.ClearSelection();
            OkButton_Click(sender, e);
        }

        void defaultIconsThumbnailViewer_ThumbnailMouseDoubleClickEvent(object sender, ThumbnailMouseEventArgs e)
        {
            myPhotoIndexThumbnailViewer1.ClearSelection();
            OkButton_Click(sender, e);
        }

        void myPhotoIndexThumbnailViewer1_ThumbnailMouseClickEvent(object sender, ThumbnailMouseEventArgs e)
        {
            defaultIconsThumbnailViewer.ClearSelection();
        }

        void defaultIconsThumbnailViewer_ThumbnailMouseClickEvent(object sender, ThumbnailMouseEventArgs e)
        {
            myPhotoIndexThumbnailViewer1.ClearSelection();
        }
    }
}